//
// Created by Luhang on 2023/5/25/025.
//
#pragma once

#include "RBTree.h"

namespace Lu{
    template<class K>
    class set{
        struct SetKeyOfT{
            const K& operator() (const K& key){
                return key;
            }
        };

    public:
        //保证Key不能被修改
        typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
        typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;

        //_t是普通对象，调用begin是普通迭代器，返回的也是普通迭代器
        //此处的iterator是const迭代器，存在类型转换问题
        //解决方法：给迭代器写一个构造函数（用普通迭代器构造const迭代器）
        iterator begin(){
            return _t.begin();
        }

        iterator end(){
            return _t.end();
        }

        pair<iterator, bool> insert(const K& key){
            return _t.Insert(key);
        }

    private:
        RBTree<K, K, SetKeyOfT> _t;
    };

    void test_set1() {
        int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
        set<int> s;
        for (auto e: a) {
            s.insert(e);
        }

        set<int>::iterator it = s.begin();
        while (it != s.end()) {
            cout << *it << " ";
            //*it = 1;

            ++it;
        }
        cout << endl;

        for (auto e: s) {
            cout << e << " ";
        }
        cout << endl;
    }
}